﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WucCloud.Utility
{
    /// <summary>
    /// 统一响应结果
    /// </summary>
    public class UnifyResult
    {
        /// <summary>
        /// 无参构造
        /// </summary>
        public UnifyResult()
        {
            StatusCode = ResultCode.Success;
            Message = StatusCode.Description();
        }

        /// <summary>
        /// 有参构造
        /// </summary>
        /// <param name="code">返回的状态码</param>
        /// <param name="message">返回的提示消息</param>
        public UnifyResult(ResultCode code, object message = null)
        {
            StatusCode = code;
            Message = message ?? code.Description();
        }

        /// <summary>
        /// 有参构造
        /// </summary>
        /// <param name="message">返回的提示消息</param>
        /// <param name="code">返回的状态码</param>
        public UnifyResult(object message, ResultCode code = ResultCode.Success)
        {
            Message = message;
            StatusCode = code;
        }

        /// <summary>
        /// 返回的提示消息
        /// </summary>
        public object Message { get; set; }

        /// <summary>
        /// 返回的状态值
        /// </summary>
        public ResultCode StatusCode { get; set; }


        /// <summary>
        /// 返回错误消息
        /// </summary>
        /// <param name="message"></param>

        public static implicit operator UnifyResult(string message)
        {
            return new UnifyResult 
            { 
                Message = message, 
                StatusCode = ResultCode.ValidError 
            };
        }

        /// <summary>
        /// 是否成功
        /// </summary>
        /// <param name="success"></param>
        public static implicit operator UnifyResult(bool success)
        {
            return new UnifyResult
            {
                StatusCode = ResultCode.Success,
                Message = success ? ResultCode.Success.Description() : ResultCode.ValidError.Description()
            };
        }

        /// <summary>
        /// 异常消息
        /// </summary>
        /// <param name="exception"></param>
        public static implicit operator UnifyResult(Exception exception)
        {
            return new UnifyResult
            {
                StatusCode = ResultCode.Error,
                Message = exception.Message
            };
        }

        /// <summary>
        /// 枚举消息
        /// </summary>
        /// <param name="code"></param>
        public static implicit operator UnifyResult(ResultCode code)
        {
            return new UnifyResult
            {
                StatusCode = code,
                Message = code.Description()
            };
        }
    }
}
